package com.example.swaggerDemo.controller.filter;


import com.example.swaggerDemo.util.SessionUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 拦截器，登录检查
 */

public class LoginHandlerInterceptor extends HandlerInterceptorAdapter {
    // 目标方法执行之前
    @Override
    @CrossOrigin(origins = "*")
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginUser");
        // 如果获取的request的session中的loginUser参数为空（未登录），就返回登录页，否则放行访问
        String upopenid=   request.getParameter("upopenid");
        System.out.println("获取upopenid"+upopenid);

        if (upopenid!=null&&upopenid!="") {
            SessionUtil.setSessionAttribute("upopenid", upopenid); // 设置 Session 中的
        }
        if (user == null) {
            System.out.println("没有获取到user =="+user);
//            response.sendRedirect("index.html");
            return true;
        } else {            // 已登录，放行
            System.out.println("获取到user =="+user);
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {


    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }



}
